/*
项目:实现一可旋转的小风扇
功能:加速,加速,反转
班级:大数据1902班
姓名:王泓鑫
学号:19408080213
 */
package Fan.Fan1;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FanAnimation extends JFrame implements ActionListener,ChangeListener{
    Timer timer = new   Timer(100,this);        //成员变量属性
    JButton Start = new JButton("开始");
    JButton Stop = new JButton("停止");
    JButton Reverse = new JButton("反转");
    JButton Accelerate= new JButton("加速");
    JButton Decelerate = new JButton("减速");

    JSlider jsldSpeed = new JSlider(JSlider.VERTICAL,0,200,100);
    JTextField TextIn = new JTextField(""+jsldSpeed.getValue());
    boolean status;
    int sight;
    boolean direction;
    public FanAnimation()
    {
        setTitle("FanAnimation");          //界面初始化
        getContentPane().setLayout(new BorderLayout());
        JPanel buttons1 = new JPanel();
        JPanel buttons2 = new JPanel();
        JPanel buttons3 = new JPanel();
        buttons1.setLayout(new FlowLayout());
        buttons1.add(Start);
        buttons1.add(Stop);

        JLabel j1=new JLabel("速度为：");
        buttons3.add(j1);
        buttons3.add(TextIn);
        buttons2.add(Reverse);
        buttons2.add(Accelerate);
        buttons2.add(Decelerate);
        getContentPane().add(buttons1,BorderLayout.NORTH );
        getContentPane().add(buttons2,BorderLayout.SOUTH );
        getContentPane().add(buttons3,BorderLayout.EAST );
        getContentPane().add(new ArcsPanel(),BorderLayout.CENTER);
        getContentPane().add(jsldSpeed,BorderLayout.WEST);

        TextIn.setActionCommand("TextIn");
        TextIn.setMaximumSize(getMaximumSize());
        TextIn.addActionListener(this);
        Start.addActionListener(this);
        Stop.addActionListener(this);
        Accelerate.addActionListener(this);
        Decelerate.addActionListener(this);
        Reverse.addActionListener(this);
        timer.setActionCommand("Timer");     //设置此组件激发的操作事件的命令名称。
        timer.addActionListener(this);
        jsldSpeed.setMinimum(0);
        jsldSpeed.setMaximum(200);
        jsldSpeed.addChangeListener(this);
        jsldSpeed.setValue(100);
        jsldSpeed.setMajorTickSpacing(25);
        jsldSpeed.setPaintLabels(true);
        timer.start();
        Start.setEnabled(true);
        Stop.setEnabled(false);
        direction = true;
        sight=0;
        status=false;
    }
    private String getTextIn() {
        // TODO Auto-generated method stub
        return null;
    }
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        if(e.getActionCommand()=="开始")    //触发开始事件
        {
            Start.setEnabled(false);
            Stop.setEnabled(true);
            status=true;
            if(jsldSpeed.getValue()==0)
                status=false;
        }
        else if(e.getActionCommand().equals("停止"))  //触发停止事件
        {
            Start.setEnabled(true);
            Stop.setEnabled(false);
            status=false;
        }
        else if(e.getActionCommand()=="加速")       //触发加速事件
        {
            jsldSpeed.setValue(jsldSpeed.getValue()+10);
            double value = jsldSpeed.getValue();
            String s="" ;
            if(value>=200)
                value=199;
            s+=(int)value;
            TextIn.setText(s);
        }
        else if(e.getActionCommand() == "减速")       //触发减速事件
        {
            jsldSpeed.setValue(jsldSpeed.getValue()-10);
            double value = jsldSpeed.getValue();
            String s="" ;
            s+=(int)value;
            if(value==0)
                status=false;
            TextIn.setText(s);
        }
        else if(e.getActionCommand()=="反转")     //触发反转事件
        {
            direction = (direction?false:true);
        }
        else if(e.getActionCommand()=="TextIn")
        {
            String st = TextIn.getText();
            int integer = Integer.parseInt(st);
            if(integer>=200 )
            {
                timer.setDelay(20);
                direction=true;
                String s="" ;
                s+=200-timer.getDelay();
                TextIn.setText(s);
                jsldSpeed.setValue(199);
            }
            else if(integer<=0)
            {
                Start.setEnabled(true);
                Stop.setEnabled(false);
                status=false;
            }
            else
            {
                timer.setDelay(200-integer);
                jsldSpeed.setValue(integer);
            }
        }
        else if(e.getActionCommand()=="Timer" && status)
            repaint();
    }
    public void stateChanged(ChangeEvent e)
    {
        if(e.getSource() == jsldSpeed)
        {
            double value = jsldSpeed.getValue();
            String s="" ;
            s+=(int)value;
            TextIn.setText(s);
            timer.setDelay((int)(200-value));
        }
    }
    public static void main(String [] args)
    {
        FanAnimation My = new FanAnimation();
        My.setDefaultCloseOperation(EXIT_ON_CLOSE);
        My.setSize(500, 400);
        My.setVisible(true);
    }
    public class ArcsPanel extends JPanel{            //绘制风扇叶片
        public void paintComponent(Graphics g)
        {
            super.paintComponents(g);
            int cx = getWidth()/2 ;
            int cy = getHeight()/2;
            int radius = (int)(Math.min(cx, cy)*0.8);
            int x= cx-radius;
            int y= cy-radius;
            g.fillArc(x, y, 2*radius, 2*radius, sight, +30);
            g.fillArc(x, y, 2*radius, 2*radius, sight+90, +30);
            g.fillArc(x, y, 2*radius, 2*radius, sight+180, +30);
            g.fillArc(x, y, 2*radius, 2*radius, sight+270, +30);
            if(direction)sight = (sight+20)%360;
            else
            {
                sight = sight-20;
                sight = (sight<0?sight+360:sight);
            }
        }
    }
}
//